﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：Order.cs
//
//文件功能描述：绑定数据到 GridView 控件并进行分页
// 
//创建标识：2007.11.14 李鹏
//创建描述：
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SmartPaging : System.Web.UI.UserControl
{
    private GridView m_GridView;
    private DataTable m_DataTable;

    /// <summary>
    /// 设置或获取要分页的 GridView 控件
    /// </summary>
    public GridView gridView
    {
        get
        {
            return m_GridView;
        }
        set
        {
            m_GridView = value;
        }
    }

    /// <summary>
    /// 设置或获取 GridView 控件的数据源
    /// </summary>
    public DataTable dataTable
    {
        get
        {
            return m_DataTable;
        }
        set
        {
            m_DataTable = value;
        }
    }

    /// <summary>
    /// 绑定数据到 GridView 控件
    /// </summary>
    public void GridviewBind()
    {
        try
        {
            m_GridView.DataSource = m_DataTable.DefaultView;
            m_GridView.DataBind();

            LblCurrentIndex.Text = "第 " + (m_GridView.PageIndex + 1).ToString() + " 页";
            LblPageCount.Text = "共 " + m_GridView.PageCount.ToString() + " 页";
            LblRecordCount.Text = "总共 " + m_DataTable.Rows.Count.ToString() + " 条";
            if (m_DataTable.Rows.Count == 0)
            {
                #region//没有记录
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;

                LblCurrentIndex.Visible = false;
                LblPageCount.Visible = false;
                LblRecordCount.Visible = false;

                LblNoRecord.Visible = true;
                #endregion
            }
            else if (m_GridView.PageCount == 1)
            {
                #region//只有一页数据
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;

                LblCurrentIndex.Visible = true;
                LblPageCount.Visible = true;
                LblRecordCount.Visible = true;

                LblNoRecord.Visible = false;
                #endregion
            }
            else
            {
                #region//有多页数据
                btnFirst.Visible = true;
                btnPrev.Visible = true;
                btnNext.Visible = true;
                btnLast.Visible = true;

                LblCurrentIndex.Visible = true;
                LblPageCount.Visible = true;
                LblRecordCount.Visible = true;

                LblNoRecord.Visible = false;
                #endregion
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// 点击分页按钮,进行分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        string arg = ((Button)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "prev"://上一页
                if (m_GridView.PageIndex > 0)
                {
                    m_GridView.PageIndex -= 1;
                }
                break;
            case "next"://下一页
                if (m_GridView.PageIndex < (m_GridView.PageCount - 1))
                {
                    m_GridView.PageIndex += 1;
                }
                break;
            case "last"://最后一页
                m_GridView.PageIndex = (m_GridView.PageCount - 1);
                break;
            default://首页
                m_GridView.PageIndex = 0;
                break;
        }

        this.GridviewBind();
    }
    
}
